package com.me.blood.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.me.ad.ADType;
import com.me.bean.Ad;
import com.me.blood.App;
import com.me.blood.R;
import com.me.blood.databinding.AcSplashBinding;
import com.me.blood.frag.BaseDialogFragment;
import com.me.blood.frag.RightDlg;
import com.me.utils.Pref;
import com.toutiao.ad.AdListener;
import com.toutiao.ad.builder.TTBuilder;
import com.toutiao.ad.item.SplashItem;
import com.toutiao.config.TTAdManagerHolder;

import java.util.Timer;
import java.util.TimerTask;

public class SplashActivity extends BaseActivity {

    AcSplashBinding binding;
    final long MAX = 5000;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = AcSplashBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        boolean agree = Pref.getInstance(this).getBoolean("agree", false);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                h.post(() -> jump(agree));
            }
        }, !agree ? 2000 : 0);
    }

    @Override
    protected boolean paddingTop() {
        return false;
    }

    void jump(boolean agree) {
        if (!agree) {
            RightDlg dlg = new RightDlg();
            dlg.setCancelable(false);
            dlg.show(getSupportFragmentManager(), "agree");
            dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                @Override
                public void onDismiss(boolean isOk, Object value) {
                    if (isOk) {
                        Pref.getInstance(App.getApp()).putBoolean("agree", true);
                        App.getApp().initSdk();
                        jump2(2000);
                    } else {
                        finish();
                    }
                }
            });
        } else {
            jump2(0);
        }
    }

    @Override
    protected void loadAd() {
    }

    private void jump2(long cost) {
        if (!App.getApp().isInit()) {
            long left = MAX - cost - 100;
            if (left > 500) {
                h.postDelayed(() -> jump2(cost + 100), 100);
                return;
            }
        }
        if (App.getApp().isEnable(ADType.SPLASH)) {
            long start = System.currentTimeMillis();
            TTAdManagerHolder.init(this, App.getApp().getAdBean().appId, getString(R.string.app_name), new TTAdSdk.Callback() {
                @Override
                public void success() {
                    loadSplash(cost + System.currentTimeMillis() - start);
                }

                @Override
                public void fail(int i, String s) {
                    long tmp = cost + System.currentTimeMillis() - start;
                    long left = MAX - tmp;
                    if (left > 600) {
                        h.postDelayed(() -> jump2(tmp + 100), 100);
                    } else {
                        h.postDelayed(() -> jumpToMainActivity(), left);
                    }
                }
            });
        } else {
            h.postDelayed(() -> jumpToMainActivity(), MAX - cost);
        }
    }

    private void loadSplash(long cost) {
        long start = System.currentTimeMillis();
        new TTBuilder().loadSplashAd(this, new Ad(App.getApp().getAdBean().splashId, ADType.SPLASH), new AdListener<SplashItem>() {
            @Override
            public void onGetAdSuccess(SplashItem ad) {
                binding.container.removeAllViews();
                ad.showAd(binding.container);
            }

            @Override
            public void onGetAdFail(String msg, int type) {
                long tmp = cost + System.currentTimeMillis() - start;
                long left = MAX - tmp;
                if (type == 0 || left <= 600) {
                    h.postDelayed(() -> jumpToMainActivity(), left);
                } else {
                    h.postDelayed(() -> loadSplash(tmp + 100), 100);
                }
            }

            @Override
            public void onAdDismiss(SplashItem ad) {
                if (App.getApp().isMultiSplash()) {
                    loadSplash(cost);
                    return;
                }
                jumpToMainActivity();
                binding.container.removeAllViews();
                ad.destroy();
            }
        });
    }

    boolean needJump = false;

    @Override
    protected void onResume() {
        super.onResume();
        if (needJump) {
            jumpToMainActivity();
        }
        needJump = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        needJump = false;
    }

    @Override
    protected ADType getXpType() {
        return null;
    }

    public void jumpToMainActivity() {
        if (!needJump) {
            needJump = true;
            return;
        }
        boolean isBack = getIntent().getIntExtra("back", 0) == 1;
        if (!isBack) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }
        finish();
    }
}
